import {
  getVideoRecording,
  getVideoRecordingCount,
  VideoRecordingDelete,
  createAddVideoInfoStatement,
  getMaxId,
  getChannelList,
} from './statements';
import { getDB } from '../../db';

//查询
export const getVideoInfoQuery = async (queryParams: LX.DBService.VideoInfoQuery) => {
  return await getVideoRecording(queryParams);
};

export const getVideoInfoQueryCount = async (queryParams: LX.DBService.VideoInfoQuery) => {
  return await getVideoRecordingCount(queryParams);
};

//删除
export const deleteVideoItem = async (data: LX.DBService.VideoInfoUpdate) => {
  const statements = VideoRecordingDelete();
  const db = getDB();
  db.transaction((data) => {
    statements.run(data);
  })(data);
};

export const getChannel = async () => {
  const statements = getChannelList();
  return await statements.all();
};

//新增
export const addVideoItem = (dataList: LX.DBService.VideoInfo[]) => {
  const insertStatement = createAddVideoInfoStatement();
  const db = getDB();
  const getMaxIdStatment = getMaxId();
  let rest = getMaxIdStatment.get();
  let maxId = '0';
  if (rest?.id !== null) {
    maxId = rest ? rest.id : '0';
  }
  db.transaction((dataList: LX.DBService.VideoInfo[]) => {
    for (const item of dataList) {
      maxId = (parseInt(maxId) + 1).toString();
      insertStatement.run({
        id: maxId,
        videoName: item.videoName,
        videoDuration: item.videoDuration,
        fileSize: item.fileSize,
        startTime: item.startTime,
        endTime: item.endTime,
        channel: item.channel,
        status: item.status,
        downloadStatus: item.downloadStatus,
        savePath: item.savePath,
        createTime: item.createTime,
        createUser: item.createUser,
      });
    }
  })(dataList);
};
